package fr.blogger.ejb.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

@Entity
@Table(name = "article")
@TableGenerator(name = "SEQUENCE_ARTICLE", initialValue = 0, table = "sequence", pkColumnName = "code", valueColumnName = "valeur", allocationSize = 1)
public class Article implements Serializable {

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQUENCE_ARTICLE")
	private Integer id;

	@Size(max = 100)
	@NotNull
	@Column(name = "nom", length = 100, nullable = false)
	private String nom;

	@Size(max = 400)
	@NotNull
	@Column(name = "contenu", length = 4000, nullable = false)
	private String contenu;

	@Size(max = 1000)
	@Column(name = "mots_cles", length = 1000, nullable = true)
	private String motsCles;
	
	@Size(max = 100)
	@NotNull
	@Column(name = "slug", length = 100, nullable = false)
	private String slug;

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_creation", nullable = false)
	private Date dateCreation;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_modification", nullable = true)
	private Date dateModification;

	@NotNull
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@JoinColumn(name = "ref_blog", nullable = false)
	private Blog blog;

	@OneToMany(cascade = CascadeType.REFRESH, mappedBy = "article")
	public List<Commentaire> listeCommentaire;

	public Article() {
		super();
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNom() {
		return this.nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getContenu() {
		return this.contenu;
	}

	public void setContenu(String contenu) {
		this.contenu = contenu;
	}

	public String getMotsCles() {
		return motsCles;
	}

	public void setMotsCles(String motsCles) {
		this.motsCles = motsCles;
	}

	public String getSlug() {
		return slug;
	}

	public void setSlug(String slug) {
		this.slug = slug;
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	public Date getDateModification() {
		return dateModification;
	}

	public void setDateModification(Date dateModification) {
		this.dateModification = dateModification;
	}

	public Blog getBlog() {
		return blog;
	}

	public void setBlog(Blog blog) {
		this.blog = blog;
	}

	public List<Commentaire> getListeCommentaire() {
		return listeCommentaire;
	}

	public void setListeCommentaire(List<Commentaire> listeCommentaire) {
		this.listeCommentaire = listeCommentaire;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		Article other = (Article) obj;
		return new EqualsBuilder().append(id, other.id).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(id).toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).toString();
	}

}
